<?php


namespace app\admin\controller;

use app\common\business\Goods as GoodsBiz;

class Goods extends AdminBase
{
    /**
     * 列表
     * @return \think\response\View
     */
    public function index(){
        //获取列表
        $title = input("param.title", "", "trim");
        $start = input("param.start", "", "trim");
        $end = input("param.end", "", "trim");
        $where = [
            ['status', 'in', [0, 1]]
        ];
        /********* 搜索条件***************/
        if (!empty($title)) {
            $where[]=['title', 'like', "%$title%"];
        }
        if (empty($start) && !empty($end)) {
            $where[] = ['create_time', 'between', [strtotime('1971'), strtotime($end)]];
        }
        if (!empty($start) && empty($end)) {
            $where[] = ['create_time', 'between', [strtotime($start), time()]];
        }
        if (!empty($start) && !empty($end)) {
            $where[] = ['create_time', 'between', [strtotime($start), strtotime($end)]];
        }
        /********* END 搜索条件***************/
        //查询的字段
        $field = 'id, title, title_cut, list_order, recommend_image, stock, create_time, status, is_index_recommend';
        $data = (new GoodsBiz())->getLists($field, $where, 5);
//        print_r($data);
        return view('', [
            'data' => $data,
            'search'=> [
                'start'=> $start,
                'end'=> $end,
                'title'=> $title,
            ]
        ]);
    }

    /**
     * 添加商品
     * @return \think\response\Json|\think\response\View
     */
    public function add() {
        if ($this->request->isPost()) {
            $data = input('post.');
            //处理category_id_path

            //category_id 是选择的最后的ID
            if (isset($data['category_id'])) {
                $category_ids = explode(',', $data['category_id']);
                foreach ($category_ids as $k=>$v) {
                    if ($v == 'undefined') {
                        unset($category_ids[$k]);
                    }
                }
                $data['category_path_id'] = implode(',', $category_ids);
                $data['category_id'] = end($category_ids);
            }

            // validate 验证数据
            $validate = (new \app\admin\validate\Goods())->scene('add');
            if (!$validate->check($data)) {
                return show(config('status.error'), $validate->getError());
            }
            //处理业务逻辑
            $res = (new GoodsBiz())->insertData($data);

            if($res) {
                return show(config('status.success'), '添加成功');
            }
            return show(config('status.error'), "添加失败");
        } else {
            return view();
        }

    }

    public function edit() {
        return view();
    }

    public function detail() {
        return view();
    }

    /**
     * 单个删除
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function delete(){
        $id = input("param.id", 0, "intval");
        if (!$id) {
            return show(config('status.error'), "参数错误");
        }
        $res = (new GoodsBiz())->delete($id);
        if($res) {
            return show(config('status.success'), "OK");
        } else {
            return show(config('status.error'), 'FAIL');
        }
    }

    /**
     * 异步排序
     * @return \think\response\Json
     */
    public function listorder() {
        $id = input("param.id", 0, "intval");
        $listorder = input("param.listorder", 0, "intval");
        if (!$id || $listorder < 0) {
            return show(config('status.error'), "参数错误");
        }

        try {
            $res = (new GoodsBiz())->listorder($id, $listorder);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }
        if($res) {
            return show(config('status.success'), "排序成功");
        } else {
            return show(config('status.error'), "排序失败");
        }
    }

    /**
     * 是否推荐
     * @return \think\response\Json
     */
    public function recommendStatus() {
        $status = input("param.status", 0, "intval");
        $id = input("param.id", 0, "intval");
        if (!$id || !in_array($status, [0, 1])) {
            return show(config('status.error'), "参数错误");
        }

        try {
            $res = (new GoodsBiz())->recommendStatus($id, $status);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }
        if($res) {
            return show(config('status.success'), "状态更新成功");
        } else {
            return show(config('status.error'), "状态更新失败");
        }
    }

    /**
     * 上/下架
     * @return \think\response\Json
     */
    public function status() {
        $status = input("param.status", 0, "intval");
        $id = input("param.id", 0, "intval");
        if (!$id || !in_array($status, [0, 1])) {
            return show(config('status.error'), "参数错误");
        }

        try {
            $res = (new GoodsBiz())->status($id, $status);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }
        if($res) {
            return show(config('status.success'), "状态更新成功");
        } else {
            return show(config('status.error'), "状态更新失败");
        }
    }


}